package com.dmall.leetcode307;

import java.util.Arrays;

public class NumArray1 {
    private int[] data;
    private int[] sum;
    public NumArray1(int[] nums) {

        data = new int[nums.length];
        for(int i = 0 ; i < nums.length ; i ++) {
            data[i] = nums[i];
        }
        sum = new int[nums.length + 1];
        sum[0] = 0;
        for(int i = 1 ; i <= nums.length ; i ++) {
            sum[i] = sum[i - 1] + nums[i - 1];
        }
    }

    public int[] getData() {
        return data;
    }

    public int[] getSum() {
        return sum;
    }

    public int sumRange(int i, int j) {
        return sum[j + 1] - sum[i];
    }

    public void update(int index, int val) {
        data[index] = val;
        for(int i = index + 1 ; i < sum.length ; i ++) {
            sum[i] = sum[i - 1] + data[i - 1];
        }
    }

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        NumArray1 numArray1 = new NumArray1(nums);
        System.out.println("data:" + Arrays.toString(numArray1.getData()));
        System.out.println("sum:" + Arrays.toString(numArray1.getSum()));
        numArray1.update(4, 3);
        System.out.println("data:" + Arrays.toString(numArray1.getData()));
        System.out.println("sum:" + Arrays.toString(numArray1.getSum()));
    }
}
